<?php

// Connect to our database
include("include/dbconnection.php");

// Include our user class
include_once("include/user.php");

$date = date("U");

$db_query = "SELECT 
    a.$TABLE_ADS_ADID, a.$TABLE_ADS_TITLE, a.$TABLE_ADS_LOCATION, 
    a.$TABLE_ADS_EXPIRYDATE, a.$TABLE_ADS_VIEWS, a.$TABLE_ADS_DESCRIPTION
    FROM $TABLE_ADS a 
    WHERE location = 3  AND 
    views != 0 AND 
	expiryDate != $date
	ORDER BY RAND() LIMIT 1";

$resultSet = mysql_query($db_query);
$results = mysql_fetch_array($resultSet);
?>
       <p>
	   <b><?php echo $results[$TABLE_ADS_TITLE]?></b>
       <br></br>
       <?php echo $results[$TABLE_ADS_DESCRIPTION]?>
       <br></br>
	   </p>
<?php

if(mysql_num_rows($resultSet) != 0)
{
$sql = "UPDATE $TABLE_ADS
       SET views=views -1  
       WHERE adID=$results[$TABLE_ADS_ADID]";
       
 			if (!mysql_query($sql))
			  {
        echo $sql;
			  die('Error: ' . mysql_error());
			  }
}



?>